package com.suisung.shopsuite.agent.configuration.rag.util;

import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.splitter.DocumentByCharacterSplitter;
import dev.langchain4j.data.document.splitter.DocumentByLineSplitter;
import dev.langchain4j.data.document.splitter.DocumentByParagraphSplitter;
import dev.langchain4j.data.document.splitter.DocumentByWordSplitter;
import dev.langchain4j.model.Tokenizer;
import dev.langchain4j.model.dashscope.QwenTokenizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class SplitterUtil {


//### 构造方法参数：
//1. `int maxSegmentSizeInTokens`：
//            - 每个分割后片段的最大token数量（必须大于0）
//            - 通过`ValidationUtils.ensureGreaterThanZero`确保值合法
//2. `int maxOverlapSizeInTokens`：
//            - 相邻片段之间的重叠token数量（必须在0和`maxSegmentSize`之间）
//            - 通过`ValidationUtils.ensureBetween`确保值在有效范围内
//3. `Tokenizer tokenizer`：
//            - 用于计算文本token数的分词器
//4. `DocumentSplitter subSplitter`：
//            - 次级分割器（用于分层分割的第二阶段）。如果为`null`，则使用默认分割器。

    @Value("${Splitter.maxSegmentSizeInTokens}")
    private int maxSegmentSizeInTokens;

    @Value("${Splitter.maxOverlapSizeInTokens}")
    private int maxOverlapSizeInTokens;

    @Value("${Splitter.isOpenTokenizer}")
    private boolean isOpenTokenizer;

    @Value("${Splitter.isSecondSplitter}")
    private boolean isSecondSplitter;

    @Value("${Splitter.Tokenizer.apiKey}")
    private String apiKey;

    @Value("${Splitter.Tokenizer.modelName}")
    private String modelName;
    protected   Tokenizer tokenizer = null;

    protected DocumentSplitter documentSplitter = null;

    @PostConstruct
    public void init(){
        if(isOpenTokenizer)tokenizer = new QwenTokenizer(apiKey,modelName);
       // if(isSecondSplitter)
    }

    public DocumentSplitter LineSplitter(){
        return new DocumentByLineSplitter(maxSegmentSizeInTokens,maxOverlapSizeInTokens,tokenizer,documentSplitter);
    }
    public DocumentSplitter CharacterSplitter(){
        return new DocumentByCharacterSplitter(maxSegmentSizeInTokens,maxOverlapSizeInTokens,tokenizer,documentSplitter);
    }
    public DocumentSplitter ParagraphSplitter(){
        return new DocumentByParagraphSplitter(maxSegmentSizeInTokens,maxOverlapSizeInTokens,tokenizer,documentSplitter);
    }
    public DocumentSplitter WordSplitter(){
        return new DocumentByWordSplitter(maxSegmentSizeInTokens,maxOverlapSizeInTokens,tokenizer,documentSplitter);
    }




public final ConstantPool pool = null;
static class ConstantPool<T extends DocumentSplitter>{

}

}
